package site.lanmushan.validation.validator;


import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import site.lanmushan.validation.annotation.PasswordPatten;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
 * @author Administrator
 */
public class PhoneValidator implements ConstraintValidator<PasswordPatten, String> {
    private String defaultMsg;

    @Override
    public void initialize(PasswordPatten annotation) {
        this.defaultMsg = annotation.message();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (ObjectUtil.isEmpty(value)) {
            return true;
        }
        if (!Validator.isMobile(value)) {
            if (ObjectUtil.isEmpty(defaultMsg)) {
                constraintValidatorContext.disableDefaultConstraintViolation();
                constraintValidatorContext.buildConstraintViolationWithTemplate("请输入正确的手机号")
                        .addConstraintViolation();
            }
            return false;
        }
        return true;
    }


}
